package com.runshu.newsmartark.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.runshu.newsmartark.entity.UserInfoEntity;

/**
 * Demo class
 *
 * @author yu
 * @date 2020/7/30
 * @decs：
 */

public class UtilSharedPreference {
    private static String LOGIN_INFO_KEY = "login_info";

    public static UserInfoEntity getUserInfo(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( "com.runshu.newsmartark", Context.MODE_PRIVATE );
        String string = sp.getString( LOGIN_INFO_KEY, "" );
        UserInfoEntity userInfoEntity = null;
        if (!string.isEmpty()) {
            userInfoEntity = new Gson().fromJson( string, UserInfoEntity.class );
        }
        return userInfoEntity;
    }

    public static void saveUserInfo(Context ctx, UserInfoEntity userInfoEntity) {
        SharedPreferences sp = ctx.getSharedPreferences( "com.runshu.newsmartark", Context.MODE_PRIVATE );
        if (userInfoEntity != null)
            sp.edit().putString( LOGIN_INFO_KEY, new Gson().toJson( userInfoEntity ) ).commit();
    }

    public static void clearUserInfo(Context ctx) {
        SharedPreferences sp = ctx.getSharedPreferences( "com.runshu.newsmartark", Context.MODE_PRIVATE );
        sp.edit().remove( LOGIN_INFO_KEY ).commit();
    }


}